package com.tengju.bff.interfaces.app.withdraw;

import com.tengju.bff.interfaces.app.withdraw.facade.DepositoryFacade;
import com.tengju.bff.interfaces.app.withdraw.vo.*;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.settlement.domain.model.account.UserIdCode;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Slf4j
@RestController
@Api(tags = "存管账户接口")
@RequestMapping("/depository")
@Validated
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DepositoryController {

    private final DepositoryFacade depositoryFacade;


    @ApiOperation(value = "白名单开内部户", notes = "白名单开内部户")
    @RequestMapping(value = "whiteList/bindCard", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<BindCardAuthInitResultVO> whiteListBindCard(@RequestBody BindCardAuthInitForm bindCardAuthInitForm) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            return ApiResponse.newSuccess(depositoryFacade.whiteListBindCard(bindCardAuthInitForm, userIdCode));
        } catch (Exception e) {
            log.error("error : ", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    @ApiOperation(value = "白名单卡信息查询", notes = "白名单卡信息查询")
    @RequestMapping(value = "whiteList/cardInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<WhiteListBindCardVO> whiteListCardInfo() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        return ApiResponse.newSuccess(depositoryFacade.whiteListCardInfo(userIdCode));
    }


    @ApiOperation(value = "绑卡状态", notes = "绑卡状态")
    @RequestMapping(value = "bindBankCard/bindStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<BindStatusVO> bindStatus() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        return ApiResponse.newSuccess(depositoryFacade.bindStatus(userIdCode));
    }

    @ApiOperation(value = "绑定银行卡页信息查询", notes = "绑定银行卡页信息查询")
    @RequestMapping(value = "bindBankCard/bindInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<BindInfoVO> bindInfo(QueryBindInfoForm form) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        return ApiResponse.newSuccess(depositoryFacade.bindInfo(userIdCode, form));
    }

    @ApiOperation(value = "发起开户绑卡", notes = "发起开户绑卡")
    @RequestMapping(value = "bindBankCard/bindInit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<BindCardAuthInitResultVO> bindCardAuthInit(@RequestBody BindCardAuthInitForm bindCardAuthInitForm) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        return ApiResponse.newSuccess(depositoryFacade.bindInit(userIdCode, servletContext.getIp(), bindCardAuthInitForm));
    }

    @ApiOperation(value = "发起开户绑卡V2", notes = "发起开户绑卡V2")
    @RequestMapping(value = "bindBankCard/bindInitV2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<BindCardAuthInitResultVO> bindCardAuthInitV2(@RequestBody BindCardAuthInitForm bindCardAuthInitForm) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        return ApiResponse.newSuccess(depositoryFacade.bindInitV2(userIdCode, servletContext.getIp(), bindCardAuthInitForm));
    }

    // todo Damu codeReview
    @ApiOperation(value = "鉴权页面信息", notes = "鉴权页面信息")
    @RequestMapping(value = "bindBankCard/authInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<AuthPublicAccountInfoVO> authInfo() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        return ApiResponse.newSuccess(depositoryFacade.authPublicAccountInfo(userIdCode));
    }


    @ApiOperation(value = "绑定银行卡鉴权验证", notes = "绑定银行卡鉴权验证")
    @RequestMapping(value = "bindBankCard/authCheck", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<AuthCheckResultVO> authCheck(@RequestBody AuthCheckForm authCheckForm) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        return ApiResponse.newSuccess(depositoryFacade.authCheck(userIdCode, authCheckForm));
    }


    @ApiOperation(value = "获得支持的银行卡列表", notes = "获得支持的银行卡列表")
    @RequestMapping(value = "bindBankCard/supportBankList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<SupportBankVO> supportBankList(@NotNull @RequestParam("cardType") Integer cardType) {
        return ApiResponse.newSuccess(depositoryFacade.supportBankList(cardType));
    }

    @ApiOperation(value = "获得成功绑定的银行卡列表", notes = "获得成功绑定的银行卡列表")
    @RequestMapping(value = "bindBankCard/boundCardList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<BoundCardVO> boundCardList() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        return ApiResponse.newSuccess(depositoryFacade.boundCardList(userIdCode));
    }

    @ApiOperation(value = "开户后在绑卡鉴权过程中选择换卡(逻辑销户)", notes = "开户后在绑卡鉴权过程中选择换卡")
    @RequestMapping(value = "bindBankCard/changeCard", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Void> bindFail() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
        depositoryFacade.bindFail(userIdCode);
        return ApiResponse.newSuccess();
    }

}
